-module(sctp_pair_dispatcher).
-behaviour(gen_server).
-include("protocol.hrl").
-export([start/0,start_link/0,init/1,handle_call/3,handle_cast/2,handle_info/2,terminate/2,code_change/3]).
-export([do/2]).
-define(SERVER,?MODULE).
-record(sctp_counter,{ok_counter = 0,mme_pool = []}).
start() ->
	gen_server:start({local, ?SERVER}, ?MODULE, [], []).
start_link() ->
	gen_server:start_link({local, ?SERVER}, ?MODULE,[],[]).
do(packet_comming,Packet_Tuple) ->
	gen_server:cast(?SERVER,{packet_comming,Packet_Tuple}).
init(_) ->
	{ok,{running,#sctp_counter{}}}.
handle_call(dump_sctp_pairs,_,State)->
	sctp_pair_server_sup:dump_sctp_pairs(),
  io:format("sctp counter ~p ~n",[State]),
	{reply,ok,State};
handle_call({add_mme,Id,MMes},_,State) ->
  io:format("undefined function in sctp_pair_dispatcher~n"),
  {reply,ok,State};
handle_call(_,_,State) ->
	{reply, {error,"no func define"}, State}.

handle_cast({packet_comming,P_Tuple},{running,Sta}) ->
	[IP_Info|_] = P_Tuple#packet_tuple.context,
	<<S_Port:16,D_Port:16,_/binary>> = P_Tuple#packet_tuple.packet,
	sctp_pair_server_sup:forward({IP_Info#ip_protocol.source_ip,S_Port,IP_Info#ip_protocol.dest_ip,D_Port},P_Tuple),
	{noreply,{running,Sta#sctp_counter{ok_counter = Sta#sctp_counter.ok_counter + 1}}};

handle_cast(_, State) ->
	{noreply,State}.
handle_info(_, State) ->
	{noreply,State}.
terminate(_, _) ->
	ok.
code_change(_, State, _) ->
	{ok,State}.
